package com.zkh.myutils.net.ftp;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.zkh.myutils.utils.ArrayUtils;
import com.zkh.myutils.utils.StringUtils;

/**
 * FTP文件
 * @author zkh
 */
public class FTPFile {
	/**
	 * 文件类型之文件（-）
	 */
	public static final int TYPE_FILE = 0;
	/**
	 * 文件类型之目录（d）
	 */
	public static final int TYPE_DIRECTORY = 1;
	/**
	 * 文件类型之符号链接（l）
	 */
	public static final int TYPE_SYMBOLIC_LINK = 2;
	/**
	 * 文件类型之其它（b:设备文件，c:字符设备文件，p:管道文件）
	 */
	public static final int TYPE_OTHER = 3;
	/**
	 * 访问权限之用户
	 */
	public static final int ACCESS_USER = 0;
	/**
	 * 访问权限之用户组
	 */
	public static final int ACCESS_GROUP = 1;
	/**
	 * 访问权限之所有
	 */
	public static final int ACCESS_ALL = 2;
	/**
	 * 权限之可读
	 */
	public static final int PERMISSION_READ = 0;
	/**
	 * 权限之可写
	 */
	public static final int PERMISSION_WRITE = 1;
	/**
	 * 权限之可执行
	 */
	public static final int EXECUTE_READ = 2;
	
	//正则
	private static Pattern pattern = Pattern.compile("(.{10}) +(\\d+) +(\\w+) +(\\w+) +(\\d+) ((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec).{9}) (.+)");
	//月份
	private static String[] months = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec".split("\\|");
	
	//权限：[拥有者, 所属组, 其它用户]
	private boolean[][] permissions = new boolean[3][3];
	//原始列表
	private String rawList;
	//文件类型，硬链接数量
	private int type = TYPE_OTHER, hardLinkCount = 0;
	//文件大小
	private long size;
	//用户、用户组、名称
	private String user, group, name;
	//日期
	private Date date;
	
	public FTPFile(String row) {
		rawList = row;
		//解析
		FTPFileInfoPart part = getPart(row);
		//保存名称和大小
		this.name = part.getFilename();
		this.size = part.getSize();
		//权限
		String permissions = part.getPermissions();
		//类型
		type = Integer.parseInt(StringUtils.decode(permissions.substring(0, 1), "-", Integer.toString(TYPE_FILE), "d", Integer.toString(TYPE_DIRECTORY), "l", Integer.toString(TYPE_SYMBOLIC_LINK), Integer.toString(TYPE_OTHER)));
		//权限[读，写，执行]
		this.permissions[0] = new boolean[] {"r".equals(permissions.substring(1, 2)), "w".equals(permissions.substring(2, 3)), "x".equals(permissions.substring(3, 4))};	//拥有者
		this.permissions[1] = new boolean[] {"r".equals(permissions.substring(4, 5)), "w".equals(permissions.substring(5, 6)), "x".equals(permissions.substring(6, 7))};	//所属组
		this.permissions[2] = new boolean[] {"r".equals(permissions.substring(7, 8)), "w".equals(permissions.substring(8, 9)), "x".equals(permissions.substring(9, 10))};	//其它用户
		//硬链接数量
		hardLinkCount = part.getHardLinkCount();
		//用户、组
		this.user = part.getUser();
		this.group = part.getGroup();
		//日期
		String[] dts = part.getDate().split(" +");
		//日历对象
		Calendar calendar = Calendar.getInstance();
		//月份
		calendar.set(Calendar.MONTH, ArrayUtils.indexOf(dts[0], months));
		calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dts[1]));
		calendar.set(Calendar.SECOND, 0);
		//年份
		if(dts[2].length()==4) {
			calendar.set(Calendar.YEAR, Integer.parseInt(dts[2]));
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
		}else {
			String[] times = dts[2].split(":");
			calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0]));
			calendar.set(Calendar.MINUTE, Integer.parseInt(times[1]));
		}
		//保存
		this.date = calendar.getTime();
	}
	/**
	 * 解析字符串
	 * @param str
	 * @return
	 */
	public static FTPFileInfoPart getPart(String str) {
		//匹配
		Matcher matcher = pattern.matcher(str);
		//初始化对象
		FTPFileInfoPart part = new FTPFileInfoPart();
		//匹配到
		if(matcher.find()) {
			//权限
			part.setPermissions(matcher.group(1));
			//大小
			part.setSize(Long.parseLong(matcher.group(5)));
			//日期
			part.setDate(matcher.group(6));
			//文件名
			part.setFilename(matcher.group(8));
			//硬链接数量
			part.setHardLinkCount(Integer.parseInt(matcher.group(2)));
			//用户
			part.setUser(matcher.group(3));
			//组
			part.setGroup(matcher.group(4));
		}
		//返回默认对象
		return part;
	}
	public String getRawList() {
		return rawList;
	}
	public void setRawList(String rawList) {
		this.rawList = rawList;
	}
	public boolean isDirectory() {
		return type == TYPE_DIRECTORY;
	}
	public boolean isFile() {
		return type == TYPE_FILE;
	}
	public boolean isSymbolicLink() {
		return type == TYPE_SYMBOLIC_LINK;
	}
	public boolean isUnknown() {
		return type == TYPE_OTHER;
	}
	public void setType(int type) {
		this.type = type;
	}
	public int getType() {
		return type;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getSize() {
		return size;
	}
	public void setSize(long size) {
		this.size = size;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getGroup() {
		return group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
	/**
	 * 获取时间，一般指最后修改时间
	 * @return
	 */
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public void setPermission(int access, int permission, boolean value) {
		this.permissions[access][permission] = value;
	}
	public boolean hasPermission(int access, int permission) {
		return this.permissions[access][permission];
	}
	public int getHardLinkCount() {
		return hardLinkCount;
	}
	public void setHardLinkCount(int hardLinkCount) {
		this.hardLinkCount = hardLinkCount;
	}
	@Override
	public String toString() {
		return "FTPFile [permissions=[" + Arrays.toString(permissions[0]) + Arrays.toString(permissions[1]) + Arrays.toString(permissions[2]) + "]"
				+ ", type=" + type + ", hardLinkCount=" + hardLinkCount
				+ ", size=" + size + ", user=" + user + ", group=" + group
				+ ", name=" + name + ", date=" + date + "]";
	}
	public String toRawString() {
		return rawList;
	}
}
